﻿#include "xmlparse.h"
#include "globaldata.h"
#pragma execution_character_set("utf-8")
XmlParse::XmlParse()
{
    file = NULL;

//    md5Instance = MD5Check::getInstance();
}

XmlParse::~XmlParse()
{
    if(NULL!=file)
    {
        delete file;
        file = NULL;
    }
}


bool XmlParse::openXml(QString fileName)
{
   // qDebug()<<"*******"<<__FUNCTION__<<"****fileName****"<<fileName<<endl;
    file = new QFile();
    file->setFileName(fileName);

    if(file->isOpen())
    {
        qDebug()<<"==========XmlParse===file already open======"<<"\n\n\n";
        file->reset();
        file->close();
    }
    if(!file->open(QFile::ReadWrite | QFile::Text))
    {
        //消息预留
        //打开ｘｍｌ文件失败
        qDebug() << QObject::tr("error::ParserXML->OpenXmlFile->file.open->%s\n") << fileName;
        return false;
    }

    if(!m_doc.setContent(file))
    {
        //消息预留
        //ｘｍｌ文件格式不正确
         qDebug() << QObject::tr("error::ParserXML->OpenXmlFile->doc.setContent\n");
         file->close();
         return false;
    }
    file->reset();

    return true;
}

void XmlParse::closeXml()
{
    QTextStream out(file);
    out.reset();
    out.setCodec("utf-8");
    m_doc.save(out,4,QDomNode::EncodingFromTextStream);
    if(NULL!=file)
    {
        file->close();
        delete file;
        file  = NULL;
    }
}

void XmlParse::closeWriteXml(QString fileName)
{
    QFile fileXml(fileName);
    if(!fileXml.open(QFile::WriteOnly | QFile::Text))
    {
        qWarning("error:ParseXml->write->fileOpen\n");
        fileXml.close();
        return;
    }
    QTextStream ts(&fileXml);
    ts.reset();
    ts.setCodec("utf-8");
    m_doc.save(ts,4,QDomNode::EncodingFromTextStream);
    fileXml.close();
}

void XmlParse::closeReadXml()
{
    file->close();
}

bool XmlParse::writeXml(QString elementName, int elementValue)
{
    int count = 0;
    QDomElement root = m_doc.documentElement();
    if("configinfo" != root.tagName())
    {
        return false;
    }
    QDomNode n = root.firstChild();
    while(!n.isNull())
    {
        QDomElement e = n.toElement();
        if(!e.isNull())
        {
            if("Parameters" == e.nodeName())
            {
                QDomNodeList list = e.childNodes();//获取所有节点
                for(int i = 0;i < list.count();++i)
                {
                    QDomNode node = list.at(i);
                    if(node.isElement())
                    {
                        ++count;
                        if(node.nodeName() == elementName)
                        {
                            QDomNode oldnode = node.firstChild();
                            oldnode.setNodeValue(QString::number(elementValue));
                            break;
                        }
                        if(count == list.count())
                        {
                            if(list.at(count -1).nodeName() != elementName)
                            {
                                //qDebug()<<"no element in this list"<<endl;
                            }
                        }
                    }
                }
            }
        }
        n = n.nextSibling();
    }

    return true;
}

bool XmlParse::writeXml(QString elementName, double elementValue)
{
    int count = 0;
    QDomElement root = m_doc.documentElement();
    if("configinfo" != root.tagName())
    {
        return false;
    }
    QDomNode n = root.firstChild();
    while(!n.isNull())
    {
        QDomElement e = n.toElement();
        if(!e.isNull())
        {
            if("Parameters" == e.nodeName())
            {
                QDomNodeList list = e.childNodes();//获取所有节点
                for(int i = 0;i < list.count();++i)
                {
                    QDomNode node = list.at(i);
                    if(node.isElement())
                    {
                        ++count;
                        if(node.nodeName() == elementName)
                        {
                            QDomNode oldnode = node.firstChild();
                            oldnode.setNodeValue(QString::number(elementValue));
                            break;
                        }
                        if(count == list.count())
                        {
                            if(list.at(count -1).nodeName() != elementName)
                            {
                                //qDebug()<<"no element in this list"<<endl;
                            }
                        }
                    }
                }
            }
        }
        n = n.nextSibling();
    }
    return true;
}

bool XmlParse::writeXml(QString elementName, float elementValue)
{
    int count = 0;
    QDomElement root = m_doc.documentElement();
    if("configinfo" != root.tagName())
    {
        return false;
    }
    QDomNode n = root.firstChild();
    while(!n.isNull())
    {
        QDomElement e = n.toElement();
        if(!e.isNull())
        {
            if("Parameters" == e.nodeName())
            {
                QDomNodeList list = e.childNodes();//获取所有节点
                for(int i = 0;i < list.count();++i)
                {
                    QDomNode node = list.at(i);
                    if(node.isElement())
                    {
                        ++count;
                        if(node.nodeName() == elementName)
                        {
                            QDomNode oldnode = node.firstChild();
                            oldnode.setNodeValue(QString::number(elementValue));
                            break;
                        }
                        if(count == list.count())
                        {
                            if(list.at(count -1).nodeName() != elementName)
                            {
                                //qDebug()<<"no element in this list"<<endl;
                            }
                        }
                    }
                }
            }
        }
        n = n.nextSibling();
    }

    return true;
}


bool XmlParse::writeXml(int robotId, QString elementName, QString elementValue)
{
    QString dataStr;

    QDomElement root = m_doc.documentElement();
    if("configinfo" != root.tagName())
    {
        return false;
    }
    QDomNode n = root.firstChild();
    while(!n.isNull())
    {
        QDomElement e = n.toElement();
        if(!e.isNull())
        {
            if("Parameters" == e.nodeName())
            {
                QDomNodeList list = e.childNodes();//获取所有节点
                for(int i = 0;i < list.count();++i)
                {
                    QDomNode node = list.at(i);
                    if(node.isElement())
                    {
                        if(-1 == robotId)
                        {
                            dataStr = "GlobalData";
                        }
                        else
                        {
                            dataStr = "LocalData";
                        }

                        if(dataStr == node.nodeName())
                        {
                            if(-1 == robotId)
                            {
                                QDomElement nodeElement = node.toElement();
                                if(nodeElement.hasChildNodes())
                                {
                                    QDomNodeList globalDataList = nodeElement.childNodes();
                                    int count = 0;
                                    for(int j = 0; j < globalDataList.count();j++)
                                    {
                                        QDomNode globalDataNode = globalDataList.at(j);
                                        if(globalDataNode.isElement())
                                        {
                                                ++count;
                                               if(elementName == globalDataNode.nodeName())
                                               {
                                                   QDomNode oldNode = globalDataNode.firstChild();
                                                   oldNode.setNodeValue(elementValue);
                                                   break;
                                               }

                                               if(count == globalDataList.count())
                                               {
                                                   if(globalDataList.at(count -1).nodeName() != elementName)
                                                   {
                                                       qDebug()<<"******no element in this list******"<<endl;
                                                   }
                                               }
                                        }
                                    }
                                }
                            }//全据数据的MD5值
                            else
                            {
                                QString robotStr = "robot" + QString::number(robotId);
                                 QDomElement nodeElement = node.toElement();
                                 if(nodeElement.hasChildNodes())
                                 {
                                     QDomNodeList localDataList = nodeElement.childNodes();
                                     for(int j = 0; j < localDataList.count();j++)
                                     {
                                         QDomNode localDataNode = localDataList.at(j);
                                         if(localDataNode.isElement())
                                         {
                                             if(robotStr == localDataNode.nodeName())
                                             {
                                                 QDomElement robotElement = localDataNode.toElement();
                                                 if(robotElement.hasChildNodes())
                                                 {
                                                     int count = 0;
                                                     QDomNodeList  robotList = robotElement.childNodes();
                                                     for(int k = 0; k < robotList.count();k++)
                                                     {
                                                         QDomNode robotNode = robotList.at(k);
                                                         if(robotNode.isElement())
                                                         {
                                                             ++count;
                                                             if(elementName == robotNode.nodeName())
                                                             {
                                                                 QDomNode oldNode = robotNode.firstChild();
                                                                 oldNode.setNodeValue(elementValue);
                                                                 break;
                                                             }

                                                             if(count == robotList.count())
                                                             {
                                                                 if(robotList.at(count -1).nodeName() != elementName)
                                                                 {
                                                                     qDebug()<<"--------no element in this list------"<<endl;
                                                                 }
                                                             }
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                            }//局部机器人的MD5值
                        }
                    }
                }
            }
        }
        n = n.nextSibling();
    }

    return true;
}


bool XmlParse::writeXml(QString elementName, long long elementValue)
{
    int count = 0;
    QDomElement root = m_doc.documentElement();
    if("configinfo" != root.tagName())
    {
        return false;
    }
    QDomNode n = root.firstChild();
    while(!n.isNull())
    {
        QDomElement e = n.toElement();
        if(!e.isNull())
        {
            if("Parameters" == e.nodeName())
            {
                QDomNodeList list = e.childNodes();//获取所有节点
                for(int i = 0;i < list.count();++i)
                {
                    QDomNode node = list.at(i);
                    if(node.isElement())
                    {
                        ++count;
                        if(node.nodeName() == elementName)
                        {
                            QDomNode oldnode = node.firstChild();
                            oldnode.setNodeValue(QString::number(elementValue));
                            break;
                        }
                        if(count == list.count())
                        {
                            if(list.at(count -1).nodeName() != elementName)
                            {
                                //qDebug()<<"no element in this list"<<endl;
                            }
                        }
                    }
                }
            }
        }
        n = n.nextSibling();
    }

    return true;
}

bool XmlParse::writeXml(QString elementName, QString elementValue)
{
    int count = 0;
    QDomElement root = m_doc.documentElement();
    if("configinfo" != root.tagName())
    {
        return false;
    }
    QDomNode n = root.firstChild();
    while(!n.isNull())
    {
        QDomElement e = n.toElement();
        if(!e.isNull())
        {
            if("Parameters" == e.nodeName())
            {
                QDomNodeList list = e.childNodes();//获取所有节点
                for(int i = 0;i < list.count();++i)
                {
                    QDomNode node = list.at(i);
                    if(node.isElement())
                    {
                        ++count;
                        if(node.nodeName() == elementName)
                        {
                            QDomNode oldnode = node.firstChild();
                            oldnode.setNodeValue(elementValue);
                            break;
                        }
                        if(count == list.count())
                        {
                            if(list.at(count -1).nodeName() != elementName)
                            {
                                //qDebug()<<"no element in this list"<<endl;
                            }
                        }
                    }
                }
            }
        }
        n = n.nextSibling();
    }

    return true;
}

/*
bool XmlParse::writeXml(QString elementName, QVariant elementValue)
{
    int count = 0;
    QDomElement root = m_doc.documentElement();
    if(root.tagName() != "configinfo")
    {
        return false;
    }
    QDomNode n = root.firstChild();
    while(!n.isNull())
    {
        QDomElement e = n.toElement();
        if(!e.isNull())
        {
            if(e.nodeName() == "Parameters")
            {
                QDomNodeList list = e.childNodes();//获取所有节点
                for(int i = 0;i < list.count();++i)
                {
                    QDomNode node = list.at(i);
                    if(node.isElement())
                    {
                        ++count;
                        if(node.nodeName() == elementName)
                        {
                            QDomNode oldnode = node.firstChild();
                            oldnode.setNodeValue(elementValue.toString());
                            break;
                        }
                        if(count == list.count())
                        {
                            if(list.at(count -1).nodeName() != elementName)
                            {
                                //qDebug()<<"no element in this list"<<endl;
                            }
                        }
                    }
                }
            }
        }
        n = n.nextSibling();
    }

    return true;
}
*/

void XmlParse::readXml(QString elementName,int &number)
{
    ////qDebug()<<"__FUNCTION__$$$$$$$$$$"<<__FUNCTION__<<"__LINE__%%%%%%%%%%%"<<__LINE__<<endl;
    int count = 0;
    //修改保存xml
   QDomElement root = m_doc.documentElement();
   if("configinfo" != root.tagName())
       return;
   QDomNode n = root.firstChild();
   while ( !n.isNull() )
   {
       QDomElement e = n.toElement();
       if( !e.isNull())
       {
           if("Parameters" == e.nodeName())
           {
                QDomNodeList list = e.childNodes();//获取所有节点
                for(int i = 0;i < list.count();++i)
                {
                    QDomNode node = list.at(i);
                    if(node.isElement())
                    {
                        ++count;
                        if(node.nodeName() == elementName)
                        {
                            QDomNode oldnode = node.firstChild();

                            if(oldnode.nodeValue().contains("#x"))
                            {
                                QString str = oldnode.nodeValue().replace("#x","");
                                bool ok;
                                number = str.toInt(&ok,16);
                                ////qDebug() << "#x---------after replace*****"<<str<<"number == "<<number<<endl;
                            }
                            else
                            {
                                number = oldnode.nodeValue().toInt();
                                ////qDebug() << "#x---------after replace*****number == "<<number<<endl;
                            }
                            break;
                        }
                        if(count == list.count())
                        {
                            if(list.at(count -1).nodeName() != elementName)
                            {
                                //qDebug()<<"no element in this list"<<endl;
                            }
                        }
                    }
                }
           }
        }
       n = n.nextSibling();
   }

}

void XmlParse::readXml(QString elementName,float &number)
{
    ////qDebug()<<"__FUNCTION__$$$$$$$$$$"<<__FUNCTION__<<"__LINE__%%%%%%%%%%%"<<__LINE__<<endl;
    //修改保存xml
    int count = 0;
   QDomElement root = m_doc.documentElement();
   if("configinfo" != root.tagName())
       return ;
   QDomNode n = root.firstChild();
   while ( !n.isNull() )
   {
       QDomElement e = n.toElement();
       if( !e.isNull())
       {
           if("Parameters" == e.nodeName())
           {
                QDomNodeList list = e.childNodes();//获取所有节点
                for(int i = 0;i < list.count();++i)
                {
                    QDomNode node = list.at(i);
                    if(node.isElement())
                    {
                        ++count;
                        if(node.nodeName() == elementName)
                        {
                            QDomNode oldnode = node.firstChild();

                            number = oldnode.nodeValue().toFloat();
                            break;
                            //return oldnode.nodeValue();
                            //return oldnode.nodeValue().toDouble();
                        }
                        if(count == list.count())
                        {
                            if(list.at(count -1).nodeName() != elementName)
                            {
                                //qDebug()<<"no element in this list"<<endl;
                            }
                        }
                    }
                }
           }
        }
       n = n.nextSibling();
   }

}

void XmlParse::readXml(QString elementName,double &number)
{
    ////qDebug()<<"__FUNCTION__$$$$$$$$$$"<<__FUNCTION__<<"__LINE__%%%%%%%%%%%"<<__LINE__<<endl;
    //修改保存xml
    int count = 0;
   QDomElement root = m_doc.documentElement();
   if("configinfo" != root.tagName())
       return ;
   QDomNode n = root.firstChild();
   while ( !n.isNull() )
   {
       QDomElement e = n.toElement();
       if( !e.isNull())
       {
           if("Parameters" == e.nodeName())
           {
                QDomNodeList list = e.childNodes();//获取所有节点
                for(int i = 0;i < list.count();++i)
                {
                    QDomNode node = list.at(i);
                    if(node.isElement())
                    {
                        ++count;
                        if(node.nodeName() == elementName)
                        {
                            QDomNode oldnode = node.firstChild();

                            number = oldnode.nodeValue().toDouble();
                            break;
                            //return oldnode.nodeValue();
                            //return oldnode.nodeValue().toDouble();
                        }
                        if(count == list.count())
                        {
                            if(list.at(count -1).nodeName() != elementName)
                            {
                                //qDebug()<<"no element in this list"<<endl;
                            }
                        }
                    }
                }
           }
        }
       n = n.nextSibling();
   }

}

void XmlParse::readXml(QString elementName,QString &pwd)
{
    //修改保存xml
    int count = 0;
    QDomElement root = m_doc.documentElement();
    if("configinfo" != root.tagName())
       return ;
    QDomNode n = root.firstChild();
    while ( !n.isNull() )
    {
        QDomElement e = n.toElement();
        if( !e.isNull())
        {
           if( "Parameters" == e.nodeName())
           {
                QDomNodeList list = e.childNodes();//获取所有节点

                for(int i = 0;i < list.count();++i)
                {
                    QDomNode node = list.at(i);
                    if(node.isElement())
                    {
                        ++count;
                        qDebug()<< node.nodeName();
                        if(node.nodeName() == elementName)
                        {
                            QDomNode oldnode = node.firstChild();

                            pwd = oldnode.nodeValue();
                            break;
                            //return oldnode.nodeValue();
                            //return oldnode.nodeValue().toDouble();
                        }
                        if(count == list.count())
                        {
                            if(list.at(count -1).nodeName() != elementName)
                            {
                                //qDebug()<<"no element in this list"<<endl;
                            }
                        }
                    }
                }
           }
        }
        n = n.nextSibling();
    }
}

bool XmlParse::addPro(QString proName, QString proValue)
{
    //修改保存xml
   QDomElement root = m_doc.documentElement();
   if("configinfo" != root.tagName())
   {
        return false;
   }
   else
   {
       QDomNode n = root.firstChild();
       if(!n.isNull())
       {
           QDomElement e = n.toElement();
           if( !e.isNull())
           {
               if("Parameters" == e.nodeName())
               {
                   QDomElement newPro = m_doc.createElement(proName);
                   QDomText newProValue = m_doc.createTextNode(proValue);
                   newPro.appendChild(newProValue);
                   e.appendChild(newPro);
                   return true;
               }
            }
       }
   }
}


bool XmlParse::addPro(int robotId, QString elementName, QString elementValue)
{
    //注意当robot不存在示需要添加新的节点
    //修改保存xml
    QString robotStr = "robot";

    robotStr += QString::number(robotId);

   QDomElement root = m_doc.documentElement();
   if("configinfo" != root.tagName())
   {
        return false;
   }
   else
   {
       QDomNode n = root.firstChild();
       if(!n.isNull())
       {
           QDomElement e = n.toElement();
           if( !e.isNull())
           {
               if("Parameters" == e.nodeName())
               {
                   if(e.hasChildNodes())
                   {
                       QDomNodeList parameterList = e.childNodes();
                       for(int i = 0; i < parameterList.count();i++)
                       {
                           QDomNode parameterNode = parameterList.at(i);
                           if(parameterNode.isElement())
                           {
                               if(-1 == robotId)
                               {
                                   //不存在全局的该文件的MD5属性值，需要添加该属性值
                                   QDomElement paraElement = parameterNode.toElement();
                                   if("GlobalData" == paraElement.nodeName())
                                   {
                                           qDebug() << "&&&&&&paraElement&&&&&&="<<paraElement.nodeName()<<endl;
                                           QDomElement newPro = m_doc.createElement(elementName);
                                           QDomText newProValue = m_doc.createTextNode(elementValue);
                                           newPro.appendChild(newProValue);
                                           parameterNode.appendChild(newPro);

                                           return true;
                                   }
                               }
                               else
                               {
                                    QDomElement paraElement = parameterNode.toElement();
                                   if("LocalData" == paraElement.nodeName())
                                   {
                                       qDebug() << "========paraElement======="<<paraElement.nodeName()<<endl;
                                       if(paraElement.hasChildNodes())
                                       {
                                           QDomNodeList localDataList = paraElement.childNodes();
                                           for(int j = 0; j < localDataList.count();j++)
                                           {
                                               QDomNode localDataNode = localDataList.at(j);
                                               if(robotStr == localDataNode.nodeName())
                                               {
                                                   //存在当前添加的机器人号码，不存在该文件的MD5属性，需要添加该文件的MD5属性值
                                                   QDomElement newPro = m_doc.createElement(elementName);
                                                   QDomText newProValue = m_doc.createTextNode(elementValue);
                                                   newPro.appendChild(newProValue);
                                                   localDataNode.appendChild(newPro);

                                                   return true;
                                               }
                                               else
                                               {
                                                   if(j == localDataList.count() - 1)
                                                   {
                                                       //不存在当前添加的机器人号码，需要添加机器人节点
                                                       QDomElement newPro = m_doc.createElement(robotStr);
                                                       QDomElement newChildPro = m_doc.createElement(elementName);
                                                       QDomText newChildProValue = m_doc.createTextNode(elementValue);
                                                       newChildPro.appendChild(newChildProValue);
                                                       newPro.appendChild(newChildPro);
                                                       parameterNode.appendChild(newPro);

                                                       return true;
                                                   }
                                               }
                                           }
                                       }
                                       else
                                       {
                                            //不存在当前添加的机器人号码，需要添加机器人节点
                                            QDomElement newPro = m_doc.createElement(robotStr);
                                            QDomElement newChildPro = m_doc.createElement(elementName);
                                            QDomText newChildProValue = m_doc.createTextNode(elementValue);
                                            newChildPro.appendChild(newChildProValue);
                                            newPro.appendChild(newChildPro);
                                            parameterNode.appendChild(newPro);

                                            return true;
                                       }
                                   }
                               }
                           }
                       }
                   }
               }
           }
       }
   }

}

bool XmlParse::deletePro(QString proName,int robotId)
{
    //修改保存xml

    QString robotStr = "robot";
    robotStr += QString::number(robotId);

   QDomElement root = m_doc.documentElement();
   if("configinfo" != root.tagName())
   {
        return false;
   }
   else
   {
       QDomNode n = root.firstChild();
       if(!n.isNull())
       {
           QDomElement e = n.toElement();
           if( !e.isNull())
           {
               if("Parameters" == e.nodeName())
               {
                   if(e.hasChildNodes())
                   {
                       QDomNodeList parameterList = e.childNodes();
                       for(int i = 0; i < parameterList.count();i++)
                       {
                           QDomNode parameterNode = parameterList.at(i);
                           if(parameterNode.isElement())
                           {
                               if(-1 == robotId)
                               {
                                   QDomElement paraElement = parameterNode.toElement();
                                   qDebug() << "===paraElement==="<<paraElement.nodeName()<<endl;

                                   if("GlobalData" == paraElement.nodeName() )
                                   {
                                       if(paraElement.hasChildNodes())
                                       {
                                           QDomNodeList globalDataList = paraElement.childNodes();
                                           for(int j = 0; j < globalDataList.count();j++)
                                           {
                                               QDomNode globalData = globalDataList.at(j);
                                               qDebug() << "======globalData========"<<globalData.nodeName()<<endl;
                                               if(proName == globalData.nodeName())
                                               {
                                                   paraElement.removeChild(globalData);
                                                   return true;
                                               }
                                           }
                                       }
                                       else
                                       {
                                           //报错，当前节点下没有属性节点
                                           qDebug() << "error================"<<endl;
                                       }
                                   }
                               }
                               else
                               {
                                   QDomElement paraElement = parameterNode.toElement();
                                   if("LocalData" == paraElement.nodeName())
                                   {
                                       qDebug() << "========paraElement======="<<paraElement.nodeName()<<endl;
                                       if(paraElement.hasChildNodes())
                                       {
                                           QDomNodeList localDataList  = paraElement.childNodes();
                                           for(int j = 0; j < localDataList.count(); j++)
                                           {
                                               QDomNode localData = localDataList.at(j);
                                               if(robotStr == localData.nodeName())
                                               {
                                                   QDomElement robotElement = localData.toElement();
                                                   if(robotElement.hasChildNodes())
                                                   {
                                                       QDomNodeList robotNodeList = robotElement.childNodes();
                                                       for(int k = 0; k < robotNodeList.count();k++)
                                                       {
                                                           QDomNode robotNode = robotNodeList.at(k);
                                                           if(proName == robotNode.nodeName())
                                                           {
                                                               robotElement.removeChild(robotNode);
                                                               return true;
                                                           }
                                                           else
                                                           {
                                                               if(k == robotNodeList.count() -1)
                                                               {
                                                                   //报错
                                                                   qDebug() << "error================"<<endl;
                                                               }
                                                           }
                                                       }
                                                   }
                                                   else
                                                   {
                                                       //报错
                                                       qDebug() << "error================"<<endl;
                                                   }
                                               }
                                           }
                                       }
                                   }
                               }
                           }
                       }
                   }
               }
           }
       }
   }

}

void XmlParse::getAllProList(QMap <int,QMap<QString, QString> >&proNameMap)
{
    proNameMap.clear();

    QMap <QString,QString> tmpMd5Map;
    tmpMd5Map.clear();

    int robotId = -1;

    QString proName,proValue;

    //
    QDomElement root = m_doc.documentElement();
    if("configinfo" != root.tagName())
    {
        return;
    }

    QDomNode n = root.firstChild();
    while(!n.isNull())
    {
        QDomElement e = n.toElement();
        if(!e.isNull())
        {
            if("Parameters" == e.nodeName())
            {
                QDomNodeList list = e.childNodes();//获取所有node内容
                for(int i = 0; i < list.count();++i)
                {
                    QDomNode node = list.at(i);
                    if(node.isElement())
                    {
                        proName = node.nodeName();//qstring
                        if("GlobalData" == proName)
                        {
                            robotId = -1;
                            QDomElement globalElement = node.toElement();
                            if(globalElement.hasChildNodes())
                            {
                                QDomNodeList globalNodeList =  globalElement.childNodes();
                                for(int j = 0; j < globalNodeList.count();j++)
                                {
                                    QDomNode globalNode = globalNodeList.at(j);
                                    if(globalNode.isElement())
                                    {
                                         proName = globalNode.nodeName();
                                         proValue = globalNode.firstChild().nodeValue();

                                         tmpMd5Map.insert(proName,proValue);
                                    }
                                }
                            }
                            proNameMap.insert(robotId,tmpMd5Map);
                            tmpMd5Map.clear();
                        }
                        else if("LocalData" == proName)
                        {
                            QDomElement localElement = node.toElement();
                            if(localElement.hasChildNodes())
                            {
                                QDomNodeList localNodeList =  localElement.childNodes();
                                for(int j = 0; j < localNodeList.count();j++)
                                {
                                    QString robotStr = "robot";
                                    robotId = j+1;
                                    QDomNode localNode= localNodeList.at(j);
                                    if(localNode.isElement())
                                    {
                                         proName = localNode.nodeName();
                                         robotStr = robotStr + QString::number(robotId);

                                         if(proName == robotStr)
                                         {
                                             QDomElement robotElement = localNode.toElement();
                                             if(robotElement.hasChildNodes())
                                             {
                                                 QDomNodeList robotList = robotElement.childNodes();
                                                 for(int k = 0; k < robotList.count();k++)
                                                 {
                                                     QDomNode robotNode = robotList.at(k);
                                                     if(robotNode.isElement())
                                                     {
                                                         proName  = robotNode.nodeName();
                                                         proValue = robotNode.firstChild().nodeValue();
                                                         tmpMd5Map.insert(proName,proValue);
                                                     }
                                                 }
                                             }
                                         }
                                         proNameMap.insert(robotId,tmpMd5Map);
                                         tmpMd5Map.clear();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        n = n.nextSibling();
    }
}

bool XmlParse::updateIoMap(int valueType,int logicValueIndex,QString message)
{
    qDebug()<<valueType<<":"<<logicValueIndex<<":"<<message;
    QDomElement root = m_doc.documentElement();
    if("IoDataMap" != root.tagName())
    {
         return false;
    }
    else
    {
        QDomNodeList lists = root.childNodes();
        for(int i = 0;i<lists.count();i++)
        {
            QDomNode ioData = lists.at(i);
            QDomElement ele = ioData.toElement();
            //qDebug()<<"i = "<<i<<";"<<ele.attribute("valueType")<<";"<<ele.attribute("logicValueIndex");

            if(ele.attribute("valueType") == QString::number(valueType) &&
                    ele.attribute("logicValueIndex") == QString::number(logicValueIndex) )
            {
                qDebug()<<"change io i = "<<i<<";"<<ele.attribute("valueType")<<";"<<ele.attribute("logicValueIndex");
                ele.firstChild().setNodeValue(message);
                return true;
            }
        }

    }
    return false;
}

void XmlParse::getAllIoList(QVector < QVector <QString> > &ioList)//
{
    ioList.resize(7);//null di do ai ao si so
    QDomElement root = m_doc.documentElement();
    if("IoDataMap" == root.tagName())
    {
        // 获取子节点列表
        QDomNodeList lists = root.childNodes();

        for(int i = 0;i<lists.count();i++)
        {
            // 获取其中一个子节点
            QDomNode ioData = lists.at(i);
            // 获取子节点元素
            QDomElement ele = ioData.toElement();

            // 获取子节点元素属性值
            int index = ele.attribute("valueType").toInt();
            ioList[index].append(ele.firstChild().nodeValue());
            //qDebug()<<ioList[index];
        }
    }
}

//查找节点的子节点
QDomNode XmlParse::findSubNode( const QDomNode &node, QString name , bool &ok )
{
    QDomNodeList nodelist = node.childNodes();
    int len = nodelist.length();
    for(int i=0; i<len; i++)
    {
        if( nodelist.at(i).nodeName() == name )
        {
            ok=true;
            return  nodelist.at(i);
        }
    }
    // qDebug()<<" findSubNode: fail!"<< name <<len <<node.nodeName();
    ok = false;
    return QDomNode();
}

QDomElement XmlParse::addNode( QDomNode &pnode, QString nodeName, QString val )
{
    QDomElement idNode = m_doc.createElement(nodeName);
    QDomText idNodeTxt = m_doc.createTextNode(val);
    idNode.appendChild( idNodeTxt );
    pnode.appendChild( idNode );
    return idNode;
}

